﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PMP.Data;
using PMP.Models.Shared;

namespace PMP.Controllers
{
    public class ExamMobileController : Controller
    {
        private readonly int min = 10;
        private readonly int max = 200;
        private readonly PMPContext context;

        public ExamMobileController(PMPContext context)
        {
            this.context = context;
        }

        public IActionResult Index()
        {
            return View();
        }

        public async Task<IActionResult> DailyExamAsync()
        {
            var list = await context.Exam.Where(x => x.Name.Length == 10).OrderByDescending(x => x.Name).ToListAsync();
            ViewBag.list = list;
            return View();
        }

        public async Task<IActionResult> ExamQuestAsync(int ID)
        {
            var exam = await context.Exam.SingleOrDefaultAsync(x => x.ID == ID);
            if (exam == null)
            {
                return View("Error", new ErrorModel { Msg = "数据无效" });
            }
            var list = await (from r in context.ExamQuestRel
                              join q in context.Quest on r.QID equals q.ID
                              where r.EID == exam.ID
                              orderby r.Order
                              select q).ToListAsync();
            ViewBag.exam = exam;
            ViewBag.list = list;
            return View();
        }

        public IActionResult ExamList()
        {
            return View("Error", new ErrorModel { Msg = "开发中..." });
        }

        public IActionResult Admin()
        {
            return View("Error", new ErrorModel { Msg = "开发中..." });
        }

        public IActionResult RandomIndex()
        {
            ViewBag.min = min;
            ViewBag.max = max;
            return View();
        }

        public async Task<IActionResult> RandomQuestAsync(int number)
        {
            if (number < min)
            {
                number = min;
            }
            if (number > max)
            {
                number = max;
            }
            var list = await context.Quest.OrderBy(q => Guid.NewGuid()).Take(number).ToListAsync();
            ViewBag.list = list;
            return View();
        }

    }
}